<?php
// protected/components/WebModule.php
class WebModule extends CWebModule {

    public $appId = 0;
    public $appName = 'root';
    public $appTheme = 'default';
    public $appKeyword;
    public $appDescription;
    public $ajaxFull;
    private $_assets;

    public function getAssets() {
        if ($this->_assets === null)
            $this->_assets = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias("$this->name.assets"));
        return $this->_assets;
    }

    public function beforeControllerAction($controller, $action) {
        if (parent::beforeControllerAction($controller, $action)) {
            $this->ajaxFull = (isset($_GET['ajax'])) ? 1 : 0;

            $request = strtolower(Yii::app()->request->getQuery('appName'));
            $appName = ($request == null) ? $this->appName : $request;

            $connection = Yii::app()->db;
            $sql = 'SELECT id, name, theme, keyword, description FROM {{app}} WHERE status=1 AND name=:name';
            $command = $connection->createCommand($sql);
            $command->bindParam(':name', $appName, PDO:: PARAM_STR);
            $row = $command->queryRow();

            if (!empty($row)) {
                $this->appId = $row['id'];
                $this->appName = $row['name'];
                $this->appTheme = $row['theme'];
                $this->appKeyword = $row['keyword'];
                $this->appDescription = $row['description'];
            }

            if (strstr($controller->id, '/', true) == 'backend') {
                Yii::app()->theme = (Yii::app()->user->checkAccess('admin')) ? 'admin' : 'login';
                $controller->widget('common.ext.jquery.Ajax');
            } else {
                // fix theme exist
                Yii::app()->theme = $this->appTheme;
                if (Yii::app()->theme == null)
                    Yii::app()->theme = Yii::app()->params['themeDefault'];
            }

            return true;
        }
        else
            return false;
    }
}
?>